package mamikeji.liaowa.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import mamikeji.liaowa.R;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;
import mamikeji.liaowa.utils.MD5Util;
import mamikeji.liaowa.utils.MultiStateView;

public class AppErrorActivity extends BaseActivity {
    private static final String TAG = "AppErrorActivity";
    @BindView(R.id.multiStateView_app_error)
    MultiStateView multiStateViewAppError;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_error);
        ButterKnife.bind(this);
        getBase();
        multiStateViewAppError.getView(MultiStateView.VIEW_STATE_ERROR).findViewById(R.id.button_error_refish)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        multiStateViewAppError.setViewState(MultiStateView.VIEW_STATE_LOADING);
                        requestRight();
                    }
                });
    }

    /**
     * 授权
     */
    public void requestRight() {
        final String androidID = ForPreference.getString(ForContents.GET_ANDROID_ID);
        String md5_ID = null;
        try {
            md5_ID = MD5Util.md5Encode(androidID + ForContents.SUFFIX);
        } catch (Exception e) {
            e.printStackTrace();
            ForToast.showToast("授权失败！请重新启动APP");
        }

        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.EQUIPMENTID, androidID);
        formMap.put(HttpKey.SOURCE, HttpKey.ANDROID);
        formMap.put(HttpKey.K, md5_ID);
        OkHttpUtils.post()
                .url(HttpApi.ROOT_PATH + HttpApi.LOGIN_RIGHT)
                .params(formMap)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        ForLog.i(TAG, e.getMessage());
                        multiStateViewAppError.setViewState(MultiStateView.VIEW_STATE_ERROR);
                    }

                    @Override
                    public void onResponse(String response) {
                        ForLog.i(TAG, response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String status = jsonObject.getString(HttpKey.STATUS);
                            if ("200".equals(status)) {
                                ForPreference.putString(ForContents.USER_TOKEN, jsonObject.getString(ForContents.USER_TOKEN));
                                ForPreference.putString(HttpKey.EQUIPMENTID, androidID);

                                Boolean isFirstRun = ForPreference.getBoolean(ForContents.GET_FIRST);
                                if (isFirstRun) {
                                    if (!ForPreference.getString(ForContents.USER_PHONE).equals("") && ForPreference.getString(ForContents.USER_PHONE) != null) {
                                        //其次判断用户是否登录0
                                        //如果已登录，判断用户娃数量，少于一个，跳转添加娃页面，如果已经有娃，跳转到首页
                                        if (ForPreference.getInt(ForContents.USER_COUNT) <= 0) {
                                            ForToast.showToast(R.string.fist_wa_null);
                                            startActivity(new Intent(AppErrorActivity.this, AddWaActivity.class));
                                        } else {
                                            startActivity(new Intent(AppErrorActivity.this, MainActivity.class));
                                        }
                                    } else {
                                        startActivity(new Intent(AppErrorActivity.this, FistActivity.class));
                                    }
                                } else {
                                    startActivity(new Intent(AppErrorActivity.this, WelcomeActivity.class));
                                }
                                finish();
                            }else {
                                multiStateViewAppError.setViewState(MultiStateView.VIEW_STATE_ERROR);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

}
